package com.kongge.viewdisplayframework;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.kongge.viewdisplayframeworklib.sdk.OnSDKExitListener;
import com.kongge.viewdisplayframeworklib.sdk.SDKProxy;

public class MainActivity extends Activity{

    SDKProxy sdkProxy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sdkProxy = SDKProxy.getInstance();
        sdkProxy.init(this,"global/PageConfig.json");
        sdkProxy.setOnSDKExitListener(new OnSDKExitListener() {
            @Override
            public void onExit() {
                showExitDialog();
            }
        });
        View hexinView = sdkProxy.getHexinView();

        RelativeLayout contentView = (RelativeLayout) findViewById(R.id.contentView);
        contentView.addView(hexinView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        sdkProxy.onCreate(savedInstanceState);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        if (sdkProxy != null) {
            sdkProxy.onDestroy();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (sdkProxy != null) {
            return sdkProxy.onKeyDown(keyCode, event);
        }
        return false;
    }



    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (sdkProxy != null) {
            sdkProxy.onScreenConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (sdkProxy != null) {
            sdkProxy.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (sdkProxy != null) {
            sdkProxy.onStart();
        }
    }

    @Override
    public void onRestart() {
        super.onRestart();
        if (sdkProxy != null) {
            sdkProxy.onRestart();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (sdkProxy != null) {
            sdkProxy.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (sdkProxy != null) {
            sdkProxy.onPause();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (sdkProxy != null) {
            sdkProxy.onStop();
        }
    }

    private void showExitDialog() {
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("是否退出APP?")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        System.exit(0);
                    }
                })
                .create();
        dialog.show();
    }

}
